# -*-coding:utf-8 -*-
"""
#!/usr/bin/env python
# -*- coding:utf-8 -*-
@Project : python-base
@File : upload_xingzhi.py
@Author : 华栖云-罗洋
@Time : 2023/7/15 23:54

"""
import settings
import requests
import jsonpath
import os
import time
import time
import requests
from concurrent.futures import ThreadPoolExecutor
def login():
    url = f"https://console.{settings.domin}/login/lr/login/multi-tenant-login"
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
        "Content-Type": "application/json",
        "Referer": f"https://console.{settings.domin}/login/"

    }

    data = {
        "login_type": "mobile_password",
        "login_mode": 1,
        "ras_pwd": settings.user_info.get('passwd'),
        "captcha_drive": "false",
        "validate": "",
        "user_mobile": settings.user_info.get("user_mobile")
    }
    try:
        re = requests.post(url=url, json=data,headers=header)
        loginId = jsonpath.jsonpath(re.json(), "$.data.loginId")[0]
        loginTid = jsonpath.jsonpath(re.json(), "$.data.loginTid")[0]
        return loginId, loginTid
    except Exception:
        print("账号或者密码填写有误")






def upload_file(file):
    login_info = login()
    loginId = login_info[0]
    loginTid = login_info[1]
    url = fr"https://console.{settings.domin}/auth_upload"
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
        "cookie": fr"login_cmc_id={loginId}; login_cmc_tid={loginTid}; login_cmc_type=1; login_cmc_url=https%3A%2F%2Flogin.{settings.domin}%2F; login_cmc_source=local; cmc_version=v3; group_code=1558000000; user_type=1; is_spider_user=0; PRODUCT_COUNT=2",
        "Loginid": f"{loginId}",
        "Logintid": f"{loginTid}"
    }
    videopath = f"./video/{file}"
    files = {"file": open(videopath, "rb")}
    print(f"正在上传视频{file}")
    response = requests.post(url=url, headers=header, files=files)

def upload():
    file_list = os.listdir('./video')
    starttime = time.time()

    with ThreadPoolExecutor(max_workers=10) as executor:
        executor.map(upload_file, file_list)

    endtime = time.time()
    print(f"总共耗时：{endtime - starttime}s")

if __name__ == '__main__':
    upload()